<?php

namespace App\Http\Controllers\Rank;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class RepositoriesLikesCount extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        $repositories = DB::table('love_like_counters')->select(DB::raw('love_like_counters.count,repositories.title'))->join('repositories', 'repositories.id', '=', 'love_like_counters.likeable_id')->where('likeable_type','App\Models\Repository')->orderByDesc('count')->limit(5)->get()->map(function (
            $item
        ) {
            return [$item->title, $item->count];
        });
        return view('resources.rank.repositories-likes-count')->with('repositories', $repositories);
    }
}
